package com.zzj.controller;

import cn.hutool.Hutool;
import cn.hutool.http.HttpUtil;
import com.zzj.base.Result;
import com.zzj.utils.Utils;
import lombok.extern.log4j.Log4j2;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Log4j2
@RestController
@RequestMapping("/file")
public class FileController {
    @Value("${system-params.file-save-path}")
    private String filePath;


    @Transactional
    @PostMapping("uploadImage")
    public Result<?> uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Result.error("上传失败，请选择文件");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        File folder = new File(filePath + "/images/" + simpleDateFormat.format(new Date()));
        if (!folder.exists()) {
            if (folder.mkdirs()) {
                log.info("文件夹" + folder.getAbsolutePath() + "已创建");
            } else {
                log.error("文件夹创建失败");
            }
        }
        file = Utils.rename(file);
        String fileName = file.getName();
        File dest = new File(folder, fileName);
        file.transferTo(dest);
        return Result.success("上传成功", "/blogAPI/upload/blog/images/" + simpleDateFormat.format(new Date()) + "/" + fileName);
    }

    @Transactional
    @PostMapping("uploadFile")
    public Result<?> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Result.error("上传失败，请选择文件");
        }
        String folderName = "files";
        if (Objects.requireNonNull(file.getContentType()).contains("image")) {
            folderName = "images";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        File folder = new File(filePath + "/" + folderName + "/" + simpleDateFormat.format(new Date()));
        if (!folder.exists()) {
            if (folder.mkdirs()) {
                log.info("文件夹" + folder.getAbsolutePath() + "已创建");
            } else {
                log.error("文件夹创建失败");
            }
        }
        file = Utils.rename(file);
        String fileName = file.getName();
        File dest = new File(folder, fileName);
        file.transferTo(dest);
        return Result.success("上传成功", "/blogAPI/upload/blog/" + folderName + "/" + simpleDateFormat.format(new Date()) + "/" + fileName);
    }

    @Transactional
    @PostMapping("linkToImg")
    public Result<?> linkToImg(@RequestBody Map<?, ?> map) {
        String url = (String) map.get("url");
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long l = HttpUtil.downloadFile(url, new File(filePath + "/images/" + simpleDateFormat.format(new Date()) + "/" + fileName));
        if (l == 0) {
            return Result.error("上传失败");
        }
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("originalURL", url);
        hashMap.put("url", "/blogAPI/upload/blog/images/" + simpleDateFormat.format(new Date()) + "/" + fileName);
        return Result.success("上传成功", hashMap);
    }
}
